package com.chzye.xhanddemos

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Toast
import com.rokid.axr.phone.glassvoice.RKGlassVoice
import com.rokid.axr.phone.glassvoice.RKGlassVoice.RKOfflineCommandCallback
import com.rokid.axr.phone.glassvoice.RKOfflineWords

object LothalManager {

    private const val TAG = "LothalManager"

    // 假设的 LothalClient 实例。请替换为实际的 SDK 客户端类。
    // private var lothalClient: LothalClient? = null

    private var isInitialized = false
    private var voiceCommandManger = RKGlassVoice.getInstance()
    private const val commandTag = "rokid"
    
    private var applicationContext: Context? = null

    fun start(context: Context) {
        if (isInitialized) {
            Log.w(TAG, "SDK has already been initialized.")
            return
        }
        
        // 使用 applicationContext 以避免内存泄漏
        applicationContext = context.applicationContext
        isInitialized = true
        voiceCommandManger.init()

        // 注意: 拼音之间需要有空格间隔
        var b1 = RKOfflineWords("打开相机", "da kai xiang ji",offlineCommandCallback)
        var b2 = RKOfflineWords("下一步", "xia yi bu",offlineCommandCallback)
        var b3 = RKOfflineWords("上一步", "shang yi bu",offlineCommandCallback)
        val offlinewords = arrayOf(b1, b2,b3)


        // 延迟1秒更新命令词库
        Handler(Looper.getMainLooper()).postDelayed({
            voiceCommandManger.updateOfflineWords(offlinewords)
        }, 1000)
    }

    val offlineCommandCallback = object : RKOfflineCommandCallback {
        override fun onOfflineCommandEvent(event: String?) {
            applicationContext?.let { ctx ->
                Toast.makeText(ctx, event + " 已触发", Toast.LENGTH_SHORT).show()
            }
        }
    }

    fun stop() {
        voiceCommandManger.clearOfflineWords()
        voiceCommandManger.deInit()
        applicationContext = null
        isInitialized = false
    }
}